package Servlet; /**
 * 项目名称：editServlet.java
 * 文件名称：editServlet.java
 * 类的描述：无
 * 创建时间：2025-07-18 18:02:43
 * 创建人：大帅比
 * <p>
 * 版权声明：本文件归 大帅比 所有，未经授权不得进行商业使用。
 */

import java.io.*;

import Entity.stu;
import Util.GokJDBCUtil2;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

@WebServlet(name = "editServlet", value = "/api/edit")
public class editServlet extends HttpServlet {
    public void init() {
    }

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
        if(req.getParameter("method")!=null && req.getParameter("method").equals("edit")){
            int id = Integer.valueOf(req.getParameter("id"));
            GokJDBCUtil2.update("update user set name = ?, age = ?, sex = ?,pass=? where id = ?", req.getParameter("name"), Integer.valueOf(req.getParameter("age")),req.getParameter("pass"), req.getParameter("sex"), id);
            res.sendRedirect("/app/api/testpost?page=1");
        }else {
            int id = Integer.parseInt(req.getParameter("id"));
            stu student = GokJDBCUtil2.queryForObject("select name,age,sex from user where id = ?", stu.class, id);
            req.setAttribute("stu", student);
            req.getRequestDispatcher("/Pages/edit.jsp").forward(req, res);
        }
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        res.setContentType("application/json; charset=UTF-8");
        res.setContentType("text/html; charset=UTF-8");
        String method = req.getMethod();
        if (method.equals("GET")) {
            this.doGet(req, res);
        } else if (method.equals("POST")) {
            this.doGet(req, res);
        } else {
            PrintWriter writer = res.getWriter();
            res.setCharacterEncoding("utf-8");
            res.setContentType("application/json");
            writer.println("{\"code\":\"400\",\"msg\":\"此方法不允许\"}");
            writer.close();
        }
    }

    public void destroy() {
    }
}